<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加博客或者修改</title>
</head>
<body>
    <table>
        <tr>
            <td>标题：</td>
            <td><input type="text" name="title"></td>
        </tr>
        <tr>
            <td>摘要：</td>
            <td><input type="text" name="abstract"></td>
        </tr>
        <tr>
            <td>内容：</td>
            <td><input type="text" name="content"></td>
        </tr>
        <tr>
            <td>分类：</td>
            <td><input type="text" name="class"></td>
        </tr>
        <tr>
            <td>作者：</td>
            <td><input type="text" name="author"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消" onclick="cancel()"></td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./js/init.js"></script>
    <script src="./js/api.js"></script>
    <script>
        function cancel(){
            location.href='./index.html'
        }
        
        $(function(){
            let id= getUrlParams('id');
            id=id || 0;
            
        })


        //获取URL参数
        function getUrlParams(name){
            let reg=new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            let r=window.location.search.substr(1).match(reg);
            if (r!==null) {
                return unescape(r[2])
            }else{
                return null;
            }
        }

        function save(){
            let id =getUrlParams('id');
            let obj={
              title: $('[name=title]').val(),
              abstract: $('[name=abstract]').val(),
              content: $('[name=content]').val(),
              class: $('[name=class]').val(),
              author: $('[name=author]').val(),
             
            }
            if (id) {
                updateBlog(id,obj).then((res)=>{
                    location.href='.index.html'
                })
            }else{
                let bottom={
                    title:obj.title,
                    abstract:obj.abstract,
                    content:obj.content,
                    class:obj.class,
                    author:obj.author
                }
                addBlog(bottom).then((res)=>{
                    location.href='index.html'
                })
            }
        }

    </script>
</body>
</html>